package com.transcargo.action;

import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.transcargo.errors.ErrorList;
import com.transcargo.db.db;
import com.transcargo.db.dbException;

public class EditUserAction extends AuthAction {
	private String org_name = "";
    private String org_addr = "";
    private String org_city = "";
    private String org_country = "";
    private String phone = "";
    private String email = "";
    private String web = "";
    private String skype = "";
    private String icq = "";
    private String time = "";
    private String info = "";
    private int id = 0;

    private void servletGetValues(HttpServletRequest request) throws ServletParametersException {
        org_name = request.getParameter("org_name");
        org_addr = request.getParameter("org_addr");
        org_city = request.getParameter("org_city");
        org_country = request.getParameter("org_country");
        phone = "+" + request.getParameter("phone_code") + " " + request.getParameter("phone");
        email = request.getParameter("email");
        web = request.getParameter("web");
        skype = request.getParameter("skype");
        icq = request.getParameter("icq");
        time = request.getParameter("time");
        info = request.getParameter("info");
        
        if (org_country == null) org_country = "";
        if (org_addr == null) org_addr = "";
        if (org_city == null) org_city = "";
        if (phone == null) phone = "";
        if (web == null) web = "";
        if (skype == null) skype = "";
        if (icq == null) icq = "";
        if (info == null) info = "";
        if (time == null) time = "";
    }
    private void servletValidateValues() throws ServletValidationException {
        if (org_name == null || org_name.length() == 0) throw new ServletValidationException(ErrorList.ERROR_STR_COMPANYNAME_MISSING);
        if (org_name.length() > 255) throw new ServletValidationException(ErrorList.ERROR_STR_COMPANYNAME_TOO_LONG);
        if (email == null || email.length() == 0) throw new ServletValidationException(ErrorList.ERROR_STR_EMAIL_MISSING);
        if (email.length() > 127) throw new ServletValidationException(ErrorList.ERROR_STR_EMAIL_TOO_LONG);
        if (org_country.length() == 0) throw new ServletValidationException(ErrorList.ERROR_STR_COUNTRY_MISSING);
        if (org_addr.length() > 255) throw new ServletValidationException(ErrorList.ERROR_STR_ADDRESS_TOO_LONG);
        if (org_city.length() > 255) throw new ServletValidationException(ErrorList.ERROR_STR_CITY_TOO_LONG);
        if (phone.length() > 64) throw new ServletValidationException(ErrorList.ERROR_STR_PHONE_TOO_LONG);
        if (web.length() > 255) throw new ServletValidationException(ErrorList.ERROR_STR_URL_TOO_LONG);
        if (skype.length() > 64) throw new ServletValidationException(ErrorList.ERROR_STR_SKYPE_TOO_LONG);
        if (icq.length() > 64) throw new ServletValidationException(ErrorList.ERROR_STR_ICQ_TOO_LONG);
        if (info.length() > 1024) throw new ServletValidationException(ErrorList.ERROR_STR_INFO_TOO_LONG);
    }
    private void servletExecuteAction() throws ServletActionException {
        db.PreStatement pre = null;
        list = null;
		try {
			pre = dbase.CreateStatement("SELECT UpdateUser(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            pre.Insert(User_Id);
            pre.Insert(org_name);
            pre.Insert(org_addr);
            pre.Insert(org_city);
            pre.Insert(Integer.parseInt(org_country));
            pre.Insert(phone);
            pre.Insert(email);
            pre.Insert(web);
            pre.Insert(skype);
            pre.Insert(icq);
            pre.Insert(Float.parseFloat(time));
            pre.Insert(info);
            list = dbase.execute(pre);
		} catch (SQLException e) {
			System.out.println("SQLException: " + e.getMessage());
			throw new ServletActionException();
		} catch (dbException e) {
			System.out.println("dbException: " + e.getMessage());
			throw new ServletActionException();
		}
        if (list != null) {
        	iter = list.iterator();
        	if (iter.hasNext()) {
            	String[] ctr = iter.next();
            	if (ctr[0].length() > 0) id = Integer.parseInt(ctr[0]);
        	} else throw new ServletActionException();  
        } else throw new ServletActionException();
	}
    public void processAction(HttpServletRequest request, HttpServletResponse response) throws ServletException {
		try {
            servletGetValues(request);
            servletValidateValues();
            servletExecuteAction();
            switch (id) {
            case -1:
            	throw new ServletValidationException(ErrorList.ERROR_STR_ORGNAME_EXISTS);
            case 0:
            	throw new ServletActionException();
            }		
            sendRedirect(response, "user.jsp?i=1");
	    } catch (ServletParametersException e) {
            if (e.getMessage() != null) {
                sendError(response, new Exception(e.getMessage()));
            } else {
                sendRedirect(response, "edituser.jsp");
            }
        }  catch (ServletValidationException e) {
            sendError(response, new Exception(e.getMessage()));
        }  catch (ServletActionException e) {
            sendError(response, new Exception("" + ErrorList.ERROR_STR_GENERIC + dbase.getError()));
        }
    }
}
